package com.game.yxy.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.yxy.entity.Message;
import com.game.yxy.entity.VITransactions;
import com.game.yxy.common.Result;
import com.game.yxy.mapper.VITransactionsMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/vitransactions")
@Api(tags = "虚拟交易管理") // 描述整个控制器的作用
public class VITransactionsController {
    @Resource
    VITransactionsMapper vitransactionsMapper;

    // 添加虚拟交易记录
    @PostMapping
    @ApiOperation(value = "添加虚拟交易记录", notes = "创建一个新的虚拟交易记录")
    public Result<?> save(@RequestBody VITransactions borrow) {
        vitransactionsMapper.insert(borrow);
        return Result.success();
    }

    // 更新虚拟交易记录
    @PutMapping
    @ApiOperation(value = "更新虚拟交易记录", notes = "根据ID更新虚拟交易记录")
    public Result<?> update(@RequestBody VITransactions borrow) {
        vitransactionsMapper.updateById(borrow);
        return Result.success();
    }

    // 删除虚拟交易记录
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除虚拟交易记录", notes = "根据ID删除虚拟交易记录")
    public Result<?> delete(@ApiParam(value = "虚拟交易记录ID", required = true) @PathVariable Long id) {
        vitransactionsMapper.deleteById(id);
        return Result.success();
    }

    // 查询虚拟交易记录
    @GetMapping
    @ApiOperation(value = "查询虚拟交易记录", notes = "支持名字搜索")
    public Result<?> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "50") Integer pageSize,
            @ApiParam(value = "名字", defaultValue = "") @RequestParam(defaultValue = "") String itname) {
        LambdaQueryWrapper<VITransactions> wrapper = Wrappers.<VITransactions>lambdaQuery();
        if (StrUtil.isNotBlank(itname)) {
            wrapper.like(VITransactions::getItname, itname);
        }
        Page<VITransactions> VITransactionsPage = vitransactionsMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(VITransactionsPage);
    }

    // 根据ID查询虚拟交易记录
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询虚拟交易记录", notes = "根据虚拟交易记录ID查询虚拟交易记录记录")
    public Result<?> findById(@ApiParam(value = "虚拟交易记录ID", required = true) @PathVariable Long id){
        VITransactions viTransactions = vitransactionsMapper.selectById(id);
        if (viTransactions != null) {
            return Result.success(viTransactions);
        } else {
            return Result.failure("虚拟交易记录不存在");
        }
    }
}